package com.itheima.udp2;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

/*
组播的接收端
 */
public class ServerDemo {

    public static void main(String[] args) throws IOException {
        //创建组播接收对象
        MulticastSocket ms = new MulticastSocket(8888);

        //加入组播地址  加入了群聊224.0.1.0~224.255.255.255
        ms.joinGroup(InetAddress.getByName("224.0.1.0"));
        //创建接受数据对象
        byte[] bytes= new byte[1024*64];
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
        //接受
        while (true) {
            ms.receive(dp);
            //组织数据
            int length = dp.getLength();

            System.out.println(         dp.getAddress()+":"+new String(bytes, 0, length));
        }
        //释放资源
       // ms.close();


    }
}
